package com.gigi.controller;

import com.alibaba.cola.dto.MultiResponse;
import com.alibaba.cola.dto.Response;
import com.gigi.enums.ErrorCodeEnum;
import com.gigi.model.AskForLeaveDO;
import com.gigi.model.ExamineRequest;
import com.gigi.model.ListExamineRequest;
import com.gigi.service.manual.AskForLeaveManageService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * 请假申请管理
 */
@RestController
@RequestMapping("ask-for-leave-manage")
public class AskForLeaveManageController {

    private final AskForLeaveManageService askForLeaveManageService;

    public AskForLeaveManageController(AskForLeaveManageService askForLeaveManageService) {
        this.askForLeaveManageService = askForLeaveManageService;
    }

    @ApiOperation(value = "新增")
    @PostMapping("/insert")
    public Response insert(@RequestBody @Valid AskForLeaveDO request) {
        return askForLeaveManageService.insert(request);
    }

    @ApiOperation(value = "学生请假申请列表")
    @PostMapping("/list-for-student")
    public MultiResponse<AskForLeaveDO> listForStudent(@RequestBody @Valid ListExamineRequest request) {
        return askForLeaveManageService.listForStudent(request);
    }

    @ApiOperation(value = "老师请假申请列表")
    @PostMapping("/list-for-teacher")
    public MultiResponse<AskForLeaveDO> listForTeacher(@RequestBody @Valid ListExamineRequest request) {
        return askForLeaveManageService.listForTeacher(request);
    }

    @ApiOperation(value = "审批")
    @PostMapping("/examine")
    public Response examine(@RequestBody @Valid ExamineRequest request) {
        try {
            askForLeaveManageService.examine(request);
        } catch (Exception e) {
            System.out.println(e);
            return Response.buildFailure(ErrorCodeEnum.EDIT_FAILED.getErrCode(), ErrorCodeEnum.EDIT_FAILED.getErrDesc());
        }
        return Response.buildSuccess();
    }

}
